package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysMenuEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.AppEntity;
import io.renren.modules.sys.service.AppService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 应用表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:35
 */
@RestController
@RequestMapping("sys/app")
public class AppController {
    @Autowired
    private AppService appService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:app:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = appService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:app:info")
    public R info(@PathVariable("id") Long id){
        AppEntity app = appService.selectById(id);

        return R.ok().put("app", app);
    }

    @RequestMapping("/findDesk")
    public R findDesk(){
        try{
            List<AppEntity> docks = appService.selectList(new EntityWrapper<AppEntity>()
                    .eq("type", "app")//目录
                    .eq("Enabled", "1")//一级
                    .eq("ZL", "dock")//一级
            );
            List<AppEntity> desks = appService.selectList(new EntityWrapper<AppEntity>()
                    .eq("type", "app")//目录
                    .eq("Enabled", "1")//一级
                    .eq("ZL", "desk")//一级
            );
            return R.ok().put("desks",desks).put("docks",docks);
        }catch (Exception e){

        }


        return R.ok();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:app:save")
    public R save(@RequestBody AppEntity app){
        appService.insert(app);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:app:update")
    public R update(@RequestBody AppEntity app){
        ValidatorUtils.validateEntity(app);
        appService.updateAllColumnById(app);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:app:delete")
    public R delete(@RequestBody Long[] ids){
        appService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
